<template>
    <el-form :model="form"
             :rules="rules"
             ref="formRef">
        <el-form-item label="服务项标题"
                      prop="serviceName"
                      :label-width="formLabelWidth">
            <el-input v-model="form.serviceName"></el-input>
        </el-form-item>
        <el-form-item label="服务项内容"
                      prop="description"
                      :label-width="formLabelWidth">
            <el-input v-model="form.description"></el-input>
        </el-form-item>

        <el-form-item :label-width="formLabelWidth">
            <el-button plain
                       @click="resetForm('formRef')">取消</el-button>
            <el-button type="primary"
                       @click="submitForm('formRef')">确定</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
export default {
    data() {
        return {
            form: {
                serviceName: "",
                description: ""
            },
            formLabelWidth: "150px",
            rules: {
                serviceName: [
                    { required: true, message: "请输入服务项标题", trigger: "blur" }
                ],
                description: [
                    { required: true, message: "请输入服务项内容", trigger: "blur" }
                ]
            }
        };
    },
    methods: {
        submitForm(formName) {
            this.form.serviceName = this.form.serviceName.replace(/^\s+|\s+$/g, "");
            this.form.description = this.form.description.replace(/^\s+|\s+$/g, "");
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.$service.addService(this.form).then(res => {
                        if (res.code == "B0001") {
                            this.$message.error(res.message);
                        } else {
                            this.$message({
                                message: "添加成功",
                                type: "success"
                            });
                            this.$refs[formName].resetFields();
                            this.$emit("submit", false);
                        }
                    });
                }
            });
        },
        resetForm(formName) {
            this.$emit("submit", false);
            this.$refs[formName].resetFields();
        }
    }
};
</script>

<style lang="scss">
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
}
.avatar {
    width: 100px;
    height: 100px;
    display: block;
}
</style>